package cn.edu.swufe.surverpark.struts2.interceptor;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.edu.swufe.surverpark.struts2.service.RightService;
import cn.edu.swufe.surverpark.util.ValidateUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 *@author 陈龙 E-mail:529914446@qq.com
 *@version创建时间:2015年5月6日上午5:01:36
 *类说明
 */
public class CatchUrlInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6535126776167901227L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionProxy proxy = invocation.getProxy();
		
		String ns = proxy.getNamespace();
		String actionName = proxy.getActionName();
		
		if(!ValidateUtil.isValid(ns)
				|| ns.equals("/")){
			ns = "";
		}
		String url = ns + "/" + actionName;
//		ApplicationContext ac = (ApplicationContext) invocation.getInvocationContext().getApplication().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		ServletContext sc = ServletActionContext.getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		RightService rs = (RightService) ac.getBean("rightService");
		rs.appendRightByURL(url);
		return invocation.invoke();
	}

}

